package com.gohome.gohomechat.model;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.gohome.gohomechat.R;
import com.gohome.gohomechat.application.BaseApplication;
import com.gohome.gohomechat.model.modellistener.RegisterModelInterface;
import com.gohome.gohomechat.presenter.callback.RegisterCallBack;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.SaveCallback;

import java.io.ByteArrayOutputStream;

/**
 * Created by dllo on 16/2/29.
 */
public class RegisterModel implements RegisterModelInterface {
    private Handler handler;

    @Override
    public void register(final String userName, final String userPsw, final String againUserPsw, final RegisterCallBack listener) {
        this.handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 调用sdk注册方法
                try {
                    EMChatManager.getInstance().createAccountOnServer(userName, userPsw);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (userPsw.equals(againUserPsw)) {  // 保存数据
                                final ParseObject parseObject = new ParseObject("AllUserGoHome");
                                parseObject.put("username", userName);
                                Bitmap bitmap = BitmapFactory.decodeResource(BaseApplication.getContext().getResources(), R.mipmap.defuhead);
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                                byte[] byteArray = baos.toByteArray();
                                final ParseFile parseFile = new ParseFile(userName + "head.png", byteArray);
                                parseFile.saveInBackground(new SaveCallback() {
                                    @Override
                                    public void done(ParseException e) {
                                        if (e == null) {
                                            parseObject.put("head_img", parseFile);
                                            parseObject.saveInBackground(new SaveCallback() {
                                                @Override
                                                public void done(ParseException e) {
                                                    if (e == null) {
                                                        // 保存数据
                                                        ParseObject parseObject1 = new ParseObject("PersonalDataGoHome");
                                                        parseObject1.put("username", userName);
                                                        parseObject1.put("head_img", parseFile);
                                                        parseObject1.saveInBackground(new SaveCallback() {
                                                            @Override
                                                            public void done(ParseException e) {
                                                                if (e == null)
                                                                    listener.registerSuccess();
                                                                else
                                                                    listener.registerFailed_other("注册失败  PersonalDataGoHome");
                                                            }
                                                        });
                                                    } else {
                                                        listener.registerFailed_other("注册失败 AllUserGoHome");
                                                        Log.i("wcl", e.getMessage() + "注册失败");
                                                    }
                                                }
                                            });
                                        } else {
                                            listener.registerFailed_other("注册失败");
                                        }
                                    }
                                });
                            } else listener.registerFailed_other("两次输入密码不相同!");
                        }
                    });


                } catch (final EaseMobException e) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
//注册失败
                            int errorCode = e.getErrorCode();
                            if (errorCode == EMError.NONETWORK_ERROR) {
                                listener.registerFailed_network();
                                //    Toast.makeText(getApplicationContext(), "网络异常，请检查网络！", Toast.LENGTH_SHORT).show();
                            } else if (errorCode == EMError.USER_ALREADY_EXISTS) {
                                listener.registerFailed_repeat();
                                //   Toast.makeText(getApplicationContext(), "用户已存在！", Toast.LENGTH_SHORT).show();
                            } else if (errorCode == EMError.UNAUTHORIZED) {
                                listener.registerFailed_withoutPermission();
                                //   Toast.makeText(getApplicationContext(), "注册失败，无权限！", Toast.LENGTH_SHORT).show();
                            } else {
                                listener.registerFailed_other(e.getMessage());
                                //   Toast.makeText(getApplicationContext(), "注册失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }
            }
        }).start();
    }
}
